import time

import pymysql
import pytest
from selenium import webdriver
from selenium.webdriver.edge.service import Service

from utils.driver_utils import get_driver
from utils.mysql_utils import execute_sql_file


@pytest.fixture(scope="session",autouse=True)
def data_init_and_destroy():
    execute_sql_file("./config/warehouse_and_area_init.sql", "warehouse_and_area_init")
    execute_sql_file("./config/item_type_init.sql", "item_type_init")
    yield
    execute_sql_file("./config/data_destroy.sql", "data_destroy")

@pytest.fixture(scope="function")
def driver_handler():
    driver = get_driver()
    yield driver
    driver.quit()

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item):
    outcome = yield
    res= outcome.get_result()
    # print(res,1111111111111111111111111111111111)
    if res.when == "call" and res.failed:
        params=item.funcargs
        # print(params,222222222222222222222222222222222)
        driver=params["driver_handler"]
        now_time=time.strftime("%Y-%m-%d %H_%M_%S")
        driver.save_screenshot(f"./screenshot/失败用例_{params['case']['id']}_{params['case']['title']}_{now_time}.png")
